#include <SDL/SDL.h>
#include <SDL/SDL_ttf.h>

#include "errors.h"
#include "text.h"

TTF_Font* loadFont(const char* fileName, unsigned pt) {
    static TTF_Font* first = NULL;
    TTF_Font* ret;
    if (!(ret = TTF_OpenFont(fileName, pt))) {
        fprintf(stderr, "Error loading font `%s': %s\n", fileName, TTF_GetError());
        if (!first) {
            exit(ELOAD_TTF);
        } else {
            return first;
        }
    }
    if (!first)  {
        first = ret;
    }
    return ret;
}

SDL_Surface* text(TTF_Font* font, const char* text, colour_t colour) {
    return TTF_RenderText_Blended(font, text, colourToSDL(colour));
}

SDL_Surface* text(TTF_Font* font, const char* text, colour_t fg, colour_t bg) {
    return TTF_RenderText_Shaded(font, text, colourToSDL(fg), colourToSDL(bg));
}

void drawText(SDL_Surface* dest, TTF_Font* font, const char* txt, colour_t colour, unsigned x, unsigned y) {
    SDL_Surface* render = text(font, txt, colour);
    SDL_Rect where = {x, y, 0, 0};

    SDL_BlitSurface(render, NULL, dest, &where);
    SDL_FreeSurface(render);
}
/* vim:set ts=2 sts=2 sw=2 noexpandtab: */
